#region Using

using System;
using System.IO;
using System.Web;
using System.Net;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Xml;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using BlogEngine.Core;

#endregion


namespace Controls
{
    /// <summary>
    /// Creates and displays a slideshow.
    /// </summary>
    public class DisplayPhoto : WebControl
    {
        private Guid _PhotoId = Guid.Empty;
        [BindableAttribute(true)]
        [CategoryAttribute("Appearance")]
        public Guid PhotoId
        {
            get { return _PhotoId; }
            set { _PhotoId = value; }
        }
        private string _Slug = string.Empty;
        [BindableAttribute(true)]
        [CategoryAttribute("Appearance")]
        public string Slug
        {
            get { return _Slug; }
            set { _Slug = value; }
        }

        private int _Size = 0;
        [BindableAttribute(true)]
        [CategoryAttribute("Appearance")]
        public int Size
        {
            get { return _Size; }
            set { _Size = value; }
        }

        private bool _Inline;
        [BindableAttribute(true)]
        [CategoryAttribute("Appearance")]
        public bool Inline
        {
            get { return _Inline; }
            set { _Inline = value; }
        }

        private string _FloatStyle = string.Empty;
        [BindableAttribute(true)]
        [CategoryAttribute("Appearance")]
        public string FloatStyle
        {
            get { return _FloatStyle; }
            set { _FloatStyle = value; }
        }

        private string _RelTag = string.Empty;
        [BindableAttribute(true)]
        [CategoryAttribute("Appearance")]
        public string RelTag
        {
            get { return _RelTag; }
            set { _RelTag = value; }
        }

        protected override void OnPreRender(EventArgs e)
        {
            if (BlogSettings.Instance.PhotoUseThickBox)
            {
                Utils.RequireStandardJavaScript("thickBox");
            }

            base.OnPreRender(e);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (_PhotoId == Guid.Empty && string.IsNullOrEmpty(_Slug))
            {
                return;
            }

            Photo photo = (_PhotoId != Guid.Empty) ? Photo.GetById(_PhotoId) : Photo.GetBySlug(Slug);

            if (photo != null)
            {
                string photoUrl = (_Size > 0) ? photo.GetSizedPhotoURL(_Size) : photo.ThumbNailURL;

                using (System.IO.StringWriter sw = new System.IO.StringWriter())
                {
                    HtmlImage img = new HtmlImage();
                    img.Alt = photo.Title;
                    img.Src = photoUrl;

                    if (!string.IsNullOrEmpty(this.CssClass))
                    {
                        img.Attributes.Add("class", this.CssClass);
                    }

                    if (Inline && !string.IsNullOrEmpty(_FloatStyle))
                    {
                        img.Style.Add("float", _FloatStyle);
                        img.Style.Add("display", "block");
                    }

                    //don't inline the image - render an anchor to the detail url 
                    if (!Inline)
                    {
                        HtmlAnchor anc = new HtmlAnchor();
                        anc.Controls.Add(img);

                        if (!string.IsNullOrEmpty(_FloatStyle))
                        {
                            anc.Style.Add("float", _FloatStyle);
                            img.Style.Add("display", "inline-block");
                        }

                        if (BlogSettings.Instance.PhotoUseThickBox)
                        {
                            Utils.RequireStandardJavaScript("thickBox");

                            anc.HRef = photo.GetSizedPhotoURL(800);
                            anc.Attributes.Add("class", "thickbox");

                            if (!string.IsNullOrEmpty(_RelTag))
                            {
                                anc.Attributes.Add("rel",_RelTag);
                            }

                            anc.Title = photo.Description;

                            //HtmlAnchor detail = new HtmlAnchor();
                            //detail.HRef = photo.AbsoluteLink.ToString();
                            //detail.InnerText = "View Detail";

                            //anc.Controls.Add(detail);
                        }
                        else
                        {
                            anc.HRef = photo.AbsoluteLink.ToString();
                            anc.Title = photo.Description;
                        }

                        anc.RenderControl(new HtmlTextWriter(sw));
                    }
                    else
                    {
                        img.RenderControl(new HtmlTextWriter(sw));
                    }

                    writer.Write(sw.ToString());
                }
            }
        }
    }
}